﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    /// <summary>
    /// 一个左滑显示按钮的字段控件,直接设置InnerField值
    /// </summary>
    public class PageForm_Swipeout: PageForm_CellForField
    {
        private PageForm_CellForField _field = null;
        private List<SwipeButton> _lst=new List<SwipeButton>(2);

        public PageForm_Swipeout(ViewContext viewContext) : base(viewContext)
        {
            this.Css("mobileui-form-field weui-cell weui-cell_swiped");
        }

        public PageForm_Swipeout InnerField(PageForm_CellForField field)
        {
            _field = field;

            return this;
        }

        public PageForm_Swipeout AddButton(Action<SwipeButton> btnCfg)
        {
            var cfg=new SwipeButton();
            cfg.Class("weui-swiped-btn");

            btnCfg(cfg);

            _lst.Add(cfg);

            return this;
        }

        public override MvcHtmlString Render()
        {
            /*
             <div class="weui-cell weui-cell_swiped">
                <div class="weui-cell__bd">
                  输出实际内容
                </div>
                <div class="weui-cell__ft">
                  <a class="weui-swiped-btn weui-swiped-btn_warn delete-swipeout" href="javascript:">删除</a>
                  <a class="weui-swiped-btn weui-swiped-btn_default close-swipeout" href="javascript:">关闭</a>
                </div>
              </div>
             */

            ViewContext.Writer.WriteLine($"<div {_attrs}>");
            ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

            _field.Render();

            ViewContext.Writer.WriteLine("</div> <!-- end cell__bd -->");

            if (_lst.HasData())
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

                foreach (var button in _lst)
                {
                    button.Class(styleToString(button.getButtonStyle));
                    var href = "";
                    var clickScript = "";


                    if (button.getType== ButtonTypeEnum.Link)
                    {
                        href =$"href=\"{(string.IsNullOrWhiteSpace(button.getHref) ? "javascript:void(0)" : href)}\"" ;
                    }
                    else
                    {
                        

                        clickScript = string.IsNullOrWhiteSpace(button.getOnClickScript) ? "" : button.getOnClickScript + ";";

                        if (button.getType == ButtonTypeEnum.Submit)
                        {
                            clickScript += "WebUIJS.Submit(this)";
                        }

                        clickScript = "onclick=\"" + clickScript + "\"";
                    }

                    ViewContext.Writer.WriteLine($"<a {button.Attributes} {href} {clickScript}>删除</a>");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cell__ft -->");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end cell_swiped -->");

            return MvcHtmlString.Empty;
        }

        private string styleToString(ButtonStyleType type)
        {
            switch (type)
            {
                case ButtonStyleType.Default:
                    return "weui-swiped-btn_default";
                case ButtonStyleType.Primary:
                    break;
                case ButtonStyleType.Success:
                    break;
                case ButtonStyleType.Warning:
                    return "weui-swiped-btn_warn";
                case ButtonStyleType.Danger:
                    break;
                case ButtonStyleType.Lazur:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            return "";
        }
    }

    /// <summary>
    /// 一个左滑显示按钮的容器控件
    /// </summary>
    public class PageForm_SwipeoutContainer : ContainerBase
    {
        private List<SwipeButton> _lst = new List<SwipeButton>(2);


        public PageForm_SwipeoutContainer(ViewContext viewContext) : base(viewContext)
        {
            this.Css("mobileui-form-field weui-cell weui-cell_swiped");
        }

        public PageForm_SwipeoutContainer AddButton(Action<SwipeButton> btnCfg)
        {
            var cfg = new SwipeButton();
            cfg.Class("weui-swiped-btn");

            btnCfg(cfg);

            _lst.Add(cfg);

            return this;
        }


        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end cell__bd -->");

            if (_lst.HasData())
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

                foreach (var button in _lst)
                {
                    button.Class(styleToString(button.getButtonStyle));
                    var href = "";
                    var clickScript = "";


                    if (button.getType == ButtonTypeEnum.Link)
                    {
                        href = $"href=\"{(string.IsNullOrWhiteSpace(button.getHref) ? "javascript:void(0)" : href)}\"";
                    }
                    else
                    {


                        clickScript = string.IsNullOrWhiteSpace(button.getOnClickScript) ? "" : button.getOnClickScript + ";";

                        if (button.getType == ButtonTypeEnum.Submit)
                        {
                            clickScript += "WebUIJS.Submit(this)";
                        }

                        clickScript = "onclick=\"" + clickScript + "\"";
                    }

                    ViewContext.Writer.WriteLine($"<a {button.Attributes} {href} {clickScript}>删除</a>");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cell__ft -->");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end cell_swiped -->");
        }

        internal override ContainerBase RenderInternal()
        {
            ViewContext.Writer.WriteLine($"<div {_attrs}>");
            ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

            return this;
        }

        private string styleToString(ButtonStyleType type)
        {
            switch (type)
            {
                case ButtonStyleType.Default:
                    return "weui-swiped-btn_default";
                case ButtonStyleType.Primary:
                    break;
                case ButtonStyleType.Success:
                    break;
                case ButtonStyleType.Warning:
                    return "weui-swiped-btn_warn";
                case ButtonStyleType.Danger:
                    break;
                case ButtonStyleType.Lazur:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            return "";
        }
    }
}